package com.java.module.quartz.controller.admin;

import java.util.List;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.common.lang.annotation.Log;
import com.java.common.lang.domain.AjaxResult;
import com.java.common.lang.domain.page.TableDataInfo;
import com.java.common.lang.enums.BusinessTypeEnum;
import com.java.common.lang.exception.job.TaskException;
import com.java.common.utils.SecurityUtils;
import com.java.common.utils.WebKit;
import com.java.common.utils.poi.ExcelUtil;
import com.java.module.quartz.domain.CronJob;
import com.java.module.quartz.service.ICronJobService;
import com.java.module.quartz.util.CronUtils;

/**
 * 调度任务信息操作处理
 * 
 * @author Administrator
 */
@RestController
@RequestMapping("/quartz/cronJob")
public class CronJobController {

    @Autowired
    private ICronJobService jobService;

    /**
     * 查询定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('quartz:cronJob:list')")
    @GetMapping("/list")
    public TableDataInfo list(CronJob cronJob) {
        WebKit.startPage();
        List<CronJob> list = jobService.selectJobList(cronJob);
        return WebKit.getDataTable(list);
    }

    /**
     * 导出定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('quartz:cronJob:export')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CronJob cronJob) {
        List<CronJob> list = jobService.selectJobList(cronJob);
        ExcelUtil<CronJob> util = new ExcelUtil<CronJob>(CronJob.class);
        return util.exportExcel(list, "定时任务");
    }

    /**
     * 获取定时任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('quartz:cronJob:query')")
    @GetMapping(value = "/{jobId}")
    public AjaxResult getInfo(@PathVariable("jobId") Long jobId) {
        return AjaxResult.success(jobService.selectJobById(jobId));
    }

    /**
     * 新增定时任务
     */
    @PreAuthorize("@ss.hasPermi('quartz:cronJob:add')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CronJob cronJob) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(cronJob.getCronExpression())) {
            return AjaxResult.error("cron表达式不正确");
        }
        cronJob.setCreateBy(SecurityUtils.getUsername());
        return WebKit.toAjax(jobService.insertJob(cronJob));
    }

    /**
     * 修改定时任务
     */
    @PreAuthorize("@ss.hasPermi('quartz:cronJob:edit')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CronJob cronJob) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(cronJob.getCronExpression())) {
            return AjaxResult.error("cron表达式不正确");
        }
        cronJob.setUpdateBy(SecurityUtils.getUsername());
        return WebKit.toAjax(jobService.updateJob(cronJob));
    }

    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('quartz:cronJob:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody CronJob job) throws SchedulerException {
        CronJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        return WebKit.toAjax(jobService.changeStatus(newJob));
    }

    /**
     * 定时任务立即执行一次
     */
    @PreAuthorize("@ss.hasPermi('quartz:cronJob:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/run")
    public AjaxResult run(@RequestBody CronJob job) throws SchedulerException {
        jobService.run(job);
        return AjaxResult.success();
    }

    /**
     * 删除定时任务
     */
    @PreAuthorize("@ss.hasPermi('quartz:cronJob:remove')")
    @Log(title = "定时任务", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{jobIds}")
    public AjaxResult remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException {
        jobService.deleteJobByIds(jobIds);
        return AjaxResult.success();
    }

}